package com.android.drm;

import com.android.drm.help.CustomDialog;

import android.app.Activity;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * <p>
 * <b>CLASS:</b> com.android.drm.ChartSelection
 * </p>
 * 
 * ...........................................................................
 * <p>
 * <b>DESCRIPTION:</b><br/>
 * The chart selection screen.
 * </p>
 * 
 * ...........................................................................
 * <p>
 * <b>MODIFICATION HISTORY:</b>
 * <p>
 * [2010/08/17] michellem: Initial version.
 * [2010/08/25] michellem: Added selection for Hit Bar Chart.
 * </p>
 * </p>
 * 
 * ...........................................................................
 * 
 * @author michellem
 * @since 1.0
 */

public class ChartSelection extends ListActivity {
	
	private static final int HELP_DIALOG = 1;
	private static final int ABOUT_DIALOG = 2;

	DRMApplication drmApplication;
	//String[] items = {"Bar Chart", "Hit Bar Chart", "Line Chart", "Pie Chart"};
	String[] items = {"Bar Chart", "Hit Bar Chart", "Line Chart"}; 

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.charts_list);
		setListAdapter(new IconicAdapter(this));  
		drmApplication = (DRMApplication) this.getApplication();
	}

	public void onListItemClick(ListView parent, View v, int position, long  id) { 
		String selection = parent.getItemAtPosition(position).toString();
		
		Intent intent = null;
		if (selection == "Bar Chart") {
			intent = new Intent();
		    intent.setClass(ChartSelection.this, BarChartBuilder.class);
		} else if (selection == "Hit Bar Chart") {
			intent = new Intent();
		    intent.setClass(ChartSelection.this, HitBarChartBuilder.class);
		} else if (selection == "Line Chart") {
			intent = new Intent();
			intent.setClass(ChartSelection.this, XYChartBuilder.class);
		//} else if (selection == "Pie Chart") {
			//intent = new Intent();
			//intent.setClass(ChartSelection.this, XYChartBuilder.class);
		}
		if (intent != null) {
			setIntentExtras (intent);
			startActivity(intent);
		}
	} 
	
    public Dialog onCreateDialog(int dialogId) {
    	Dialog dialog = null;
    	switch (dialogId) {
	    	case HELP_DIALOG :
				CustomDialog.Builder customBuilder = new
				CustomDialog.Builder(ChartSelection.this);
				customBuilder.setMessage(R.string.ChartSelectionHelpMsg)
					.setPositiveButton("Close", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
	            dialog = customBuilder.create();
	    		break;
	    	case ABOUT_DIALOG : 
				CustomDialog.Builder aboutBuilder = new
				CustomDialog.Builder(ChartSelection.this);
				aboutBuilder.setMessage(R.string.AboutMsg)
					.setPositiveButton("Close", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
	            dialog = aboutBuilder.create();
	    		break;
    	}
    	return dialog;
    }
    
	public void myClickHandler (View v) {
		switch (v.getId()) {
			case R.id.helpButton:
				ChartSelection.this.showDialog(HELP_DIALOG);
				break;
			case R.id.homeButton:
				finish();				
				break;
			case R.id.logo:
				ChartSelection.this.showDialog(ABOUT_DIALOG);
				break;
			default:
				break;
		}
    }

	class IconicAdapter extends ArrayAdapter<String> {
		private Activity context; 
		private int[] colors = new int[] { 0x30ffffff, 0x30808080 };

		public IconicAdapter(Activity context) { 
			super(context, R.layout.charts_listitem, items); 
			this.context = context; 
		} 

		public View getView(int position, View convertView, ViewGroup parent) { 
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
			View row = inflater.inflate(R.layout.charts_listitem, null);

			TextView label = (TextView)row.findViewById(R.id.label); 
			label.setText(items[position]); 
			int colorPos = position % colors.length;
			row.setBackgroundColor(colors[colorPos]);

			if ((items[position] == "Bar Chart") || (items[position] == "Hit Bar Chart")) { 
				ImageView icon = (ImageView)row.findViewById(R.id.icon); 
				icon.setImageResource(R.drawable.bar_chart_32); 
			} 
			else if (items[position] == "Line Chart") {
				ImageView icon = (ImageView)row.findViewById(R.id.icon); 
				icon.setImageResource(R.drawable.line_chart_32); 
			}
			else if (items[position] == "Pie Chart") {
				ImageView icon = (ImageView)row.findViewById(R.id.icon); 
				icon.setImageResource(R.drawable.pie_chart_32); 
			}
			
			return(row); 
		} 
	} 
	
	private void setIntentExtras (Intent intent) {
		intent.putExtras(getIntent());
		intent.putExtra("averageTitle", "Average Response time per Touch");
		intent.putExtra("totalTitle", "Total Response time per Test Instance");
		intent.putExtra("correctHitLegend", "Correct Hit");
	}
}